package com.fuershiye.woman.monitor.service.impl;

import com.fuershiye.woman.monitor.beans.IdNameCode;
import com.fuershiye.woman.monitor.beans.IdNamePid;
import com.fuershiye.woman.monitor.beans.Page;
import com.fuershiye.woman.monitor.dao.IndicatorDao;
import com.fuershiye.woman.monitor.model.Indicator;
import com.fuershiye.woman.monitor.service.IndicatorService;
import com.google.common.collect.Lists;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service("indicatorService")
public class IndicatorServiceImpl implements IndicatorService {
	
	//避免多次查询,请求一次后将数据缓存到内存中
	List<Indicator> allIndicator = null;
	
	@Autowired
	private IndicatorDao itDao;

	
	@Override
	public void save(Indicator indicator) {
		itDao.save(indicator);
		allIndicator = null;
	}

	@Override
	public void delete(Long id) {
		itDao.delete(id);
		allIndicator = null;
	}

	@Override
	public Indicator get(Long id) {
		Indicator result = null;
		if(allIndicator!=null){
			result = find(id, allIndicator);
		}
		if(result!=null){
			return result;
		}
		return itDao.get(id);
	}
	
	@Override
	public Indicator findByCodeForms(String code, long formsid) {
		return itDao.findByCodeForms(code, formsid);
	}
	@Override
	public Long getIdByCodeForms(String code, long formsid) {
		return itDao.getIdByCodeForms(code, formsid);
	}

	@Override
	public Indicator getTop() {
		return itDao.getTop();
	}

	@Override
	public List<Indicator> getByPid(Long pid) {
		return itDao.getByPid(pid);
	}

	@Override
	public List<Indicator> getByPidForms(Long pid, Long formsid, Integer status) {
		return itDao.listByCondition("", null, formsid, null, pid, null, null, null, status, null);
	}

	@Override
	public List<Indicator> listByCondition(Long type, Integer status, Page page) {
		return itDao.listByCondition(type, status, page);
	}

	@Override
	public List<Indicator> listByCondition(String name, Long formsid,  List<Long> types, Integer province, Integer status, Page page) {
		return itDao.listByCondition(name, null, formsid, null, null, types, province, null, status, page);
	}

	@Override
	public List<IdNamePid> listIdNameByCondition(Integer monitor, Long formsid, Long type, Integer status) {
		return itDao.listByCondition(monitor, formsid, type, status);
	}

	@Override
	public List<Indicator> listByCondition(Integer monitor, Long formsid, List<Long> type, Integer status, Page page) {
		return itDao.listByCondition("", null, formsid, monitor, null, type, null, null, status, null);
	}

	@Override
	public List<Indicator> listByCondition(Integer monitor, Long formsid, Long type, Integer status, Page page) {
		List<Long> types = Lists.newArrayList();
		types.add(type);
		return itDao.listByCondition("", null, formsid, monitor, null, types, null, null, status, null);
	}
	@Override
	public List<Indicator> listByCondition(Integer sex, Long formsid, Integer status) {
		return itDao.listByCondition("", null, formsid, null, null, null, null, sex, status, null);
	}

	@Override
	public List<IdNameCode> listAll() {
		return itDao.list();
	}
	
	@Override
	public List<Indicator> listByCondition(String name, Long formsid, Integer monitor, Long type, Integer status, Page page) {
		List<Long> types = Lists.newArrayList();
		types.add(type);
		return itDao.listByCondition(name, null, formsid, monitor, null, types, null, null, status, page);
	}

	@Override
	public List<Indicator> listByCondition(String name, Long formsid,  Integer status, Page page) {
		return itDao.listByCondition(name, null, formsid, null, null, null, null, null, status, page);
	}

	@Override
	public List<Indicator> listByCondition(String name, Long formsid, Long type, Integer province, Integer status, Page page) {
		List<Long> types = Lists.newArrayList();
		types.add(type);
		return itDao.listByCondition(name, null, formsid, null, null, types, province, null, status, page);
	}

	@Override
	public List<Indicator> listByCondition(String name, Integer country, Long formsid, Long type, Integer province, Integer status, Page page) {
		List<Long> types = Lists.newArrayList();
		types.add(type);
		return itDao.listByCondition(name, country, formsid, null, null, types, province, null, status, page);
	}
	@Override
	public List<Indicator> listByCondition(String name, Long formsid, List<Long> types, Integer province, Integer sex, Integer status, Page page) {
		return itDao.listByCondition(name, null, formsid, null, null, types, province, sex, status, page);
	}

	@Override
	public List<Indicator> listByConditionOppositeProvince(String name, Long formsid, List<Long> type, Integer province, Integer status, Page page){
		return itDao.listByConditionOppositeProvince(name, formsid, type, province, null, status, page);
	}

	@Override
	public List<IdNamePid> getAllNames() {
		return itDao.getAllNames();
	}

	@Override
	public Integer getTotal(Integer status) {
		return itDao.getTotal("", null, null, null, status);
	}
	@Override
	public Integer getTotalByCondition(Long formsid, Integer status) {
		return itDao.getTotal("", formsid, null, null, status);
	}
	@Override
	public Integer getTotal(String name, Long formsid, List<Long> types, Integer province, Integer status) {
		return itDao.getTotal(name, formsid, types, province, status);
	}
	@Override
	public Integer getTotalOppositeProvince(String name, Long formsid, List<Long> types, Integer province, Integer status) {
		return itDao.getTotalOppositeProvince(name, formsid, types, province, status);
	}

	@Override
	public Integer getTotal(String name, Long type) {
		List<Long> types = Lists.newArrayList();
		types.add(type);
		return itDao.getTotal(name, null, types, null, null);
	}
	
	@Override
    public Integer getTotal(Integer province, Integer status) {
		return itDao.getTotal("", null, null, province, status);
	}
	
	@Override
	public Integer getTotal(Long type, Integer status) {
		List<Long> types = Lists.newArrayList();
		types.add(type);
		return itDao.getTotal("", null, types, null, status);
	}

	@Override
	public Integer getTotal(Long formsid, Long type, Integer status) {
		List<Long> types = Lists.newArrayList();
		types.add(type);
		return itDao.getTotal("", formsid, types, null, status);
	}
	
	@Override
	public Indicator findParentIndicator(Indicator indicator){
    	
    	if(indicator==null){
    		return null;
    	}
    	
    	if(indicator.getPid()==null||indicator.getPid()==0){
    		return indicator;
    	}
    	Indicator now = get(indicator.getPid());

    	//终止递归并返回
    	if(now==null){
    		return indicator;
    	}
    	
    	if(now.getPid()==null||now.getPid()==0){
    		return now;
    	}
    	return findParentIndicator(now);
    }

	@Override
	public List<Indicator> getSameBloodByParent(List<Indicator> result, Indicator indicator) {

    	if(indicator==null){
    		return result;
    	}
		result.add(indicator);
    	List<Indicator> now = getByPid(indicator.getId());

    	//终止递归并返回
    	if(now.size()<1){
    		return result;
    	}
    	
    	for(Indicator nowIndicator : now){
    		getSameBloodByParent(result, nowIndicator);
    	}
    	return result;
    
	}

	@Override
	public List<Long> getIndicatorId(List<Indicator> indicatorList) {

		List<Long> indicatorIds = Lists.newArrayList();

		if (indicatorList.isEmpty()) {
			return indicatorIds;
		}

		for (Indicator indicator : indicatorList) {
			if (indicator != null && !indicatorIds.contains(indicator.getId())) {
				indicatorIds.add(indicator.getId());
			}
		}
		return indicatorIds;
	}

	@Override
	public List<Long> getTypeIdsByCondition(Long formsid, List<Long> ids) {
		return itDao.getTypeIdsByCondition(ids, formsid, null, null, null);
	}


	private Indicator find(Long id, List<Indicator> indicatorAll){
		
		if(indicatorAll==null || id==null || id ==0 || indicatorAll.size()<1){
			return null;
		}
		for (Indicator indicator : indicatorAll) {
			if(indicator!=null && id.equals(indicator.getId())){
				return indicator;
			}
		}
		return null;
		
	}
	
	private List<Long> getIds(List<IdNamePid> idNamePids){
		
		List<Long> indicatorIds = Lists.newArrayList();
		
		if(idNamePids==null || idNamePids.size()<1){
			return indicatorIds;
		}
		for (IdNamePid idNamePid : idNamePids) {
			if(idNamePid!=null && idNamePid.getId()!=null){
				indicatorIds.add(idNamePid.getId());
			}
		}
		return indicatorIds;
		
	}

	
}
